<?php
/**
 * sender.php
 *
 * Author: Lomo
 * Date: 2018-06
 * Email: lomo@lomo.space
 * URL: http://lomo.space
 *
 * Description: 消息发送者(生产者, 向mq推送消息)
 *
 */

    require_once __DIR__ .'/vendor/autoload.php';

    use PhpAmqpLib\Connection\AMQPStreamConnection;
    use PhpAmqpLib\Message\AMQPMessage;

    // 创建RabbitMQ socket 链接，使用guest账户.
    $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
    $channel = $connection->channel();

    // 声明消息队列(发送消息前必须有队列), 队列名称为lomo
    $channel->queue_declare('lomo1', false, false, false, false);
    // 消息内容
    $msg = new AMQPMessage('Hello Lomo '.rand(1, 1000));
    // 推送消息(消息发送至消息队列)
    $channel->basic_publish($msg, '', 'lomo1');

    echo 'sent msg to queue lomo1';

    // 关闭
    $channel->close();
    $connection->close();



